/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.figures.connection;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.figures.IAbstractElementFigure;
import com.cea.papyrus.core.figures.MiddleEndpointLocator;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.VectorCalculus;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
//TODO: Auto-generated Javadoc
/**
 * This the class to represent a link between two nodes.
 * It is the super class of all link figures
 * 
 * @author Patrick Tessier
 * @since
 */
public class EdgeFigure extends PolylineConnectionBase implements IFigure, IAbstractElementFigure{
	
	/**
	 * The background color.
	 */
	protected Color backgroundColor = ColorConstants.lightBlue;
	
	/**
	 * The border color.
	 */
	protected Color borderColor     = ColorConstants.black;
	
	/**
	 * The fore ground color.
	 */
	protected Color foreGroundColor = ColorConstants.black;
	
	/**
	 * The font color.
	 */
	protected Color fontColor       = ColorConstants.black;
	
	/**
	 * The Line thickness.
	 */
	protected int LineThickness = 1;
	
	/**
	 * The font size.
	 */
	protected int fontSize      = 10;
	
	/**
	 * The line stle.
	 */
	protected String lineStle;
	
	/**
	 * The font string.
	 */
	protected String fontString = "Arial";
	
	/**
	 * The font.
	 */
	protected Font font =null;
	
	/**
	 * The img.
	 */
	protected Image img = null;
	
	/**
	 * The Constructor.
	 */
	public EdgeFigure(){
		FontData[] fontdata={new FontData("Arial", 10, SWT.NORMAL)};
		this.font = PapyrusPlugin.fontManager.get(fontdata);
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#getBackgroundColor()
	 */
	/**
	 * Gets the background color.
	 * 
	 * @return the background color
	 */
	@Override
	public Color getBackgroundColor() {
		return this.backgroundColor;
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#setBackgroundColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * Sets the background color.
	 * 
	 * @param backgroundColor the background color
	 */
	@Override
	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
		super.setBackgroundColor(backgroundColor);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getBorderColor()
	 */
	/**
	 * Gets the border color.
	 * 
	 * @return the border color
	 */
	public Color getBorderColor() {
		return this.borderColor;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setBorderColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * Sets the border color.
	 * 
	 * @param borderColor the border color
	 */
	public void setBorderColor(Color borderColor) {
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setIcon(org.eclipse.swt.graphics.Image)
	 */
	/**
	 * Sets the icon.
	 * 
	 * @param img the img
	 */
	public void setIcon(Image img) {
		this.img = img;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getfigureFont()
	 */
	/**
	 * Getfigure font.
	 * 
	 * @return the figure font
	 */
	public String getfigureFont() {
		return this.fontString;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setfigureFont(java.lang.String)
	 */
	/**
	 * Setfigure font.
	 * 
	 * @param font the font
	 */
	public void setfigureFont(String font) {
		FontData[] fontdata={new FontData("Arial", 10, SWT.NORMAL)};
		this.font = PapyrusPlugin.fontManager.get(fontdata);
		this.fontString=font;
		super.setFont(this.font);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getFontSize()
	 */
	/**
	 * Gets the font size.
	 * 
	 * @return the font size
	 */
	public int getFontSize() {
		return this.fontSize;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setFontSize(int)
	 */
	/**
	 * Sets the font size.
	 * 
	 * @param fontSize the font size
	 */
	public void setFontSize(int fontSize) {
		FontData[] fontdata={new FontData("Arial", fontSize, SWT.NORMAL)};
		this.font = PapyrusPlugin.fontManager.get(fontdata);
		super.setFont(this.font);
		this.fontSize = fontSize;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getForeGroundColor()
	 */
	/**
	 * Gets the fore ground color.
	 * 
	 * @return the fore ground color
	 */
	public Color getForeGroundColor() {
		return this.foreGroundColor;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setForeGroundColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * Sets the fore ground color.
	 * 
	 * @param foreGroundColor the fore ground color
	 */
	public void setForeGroundColor(Color foreGroundColor) {
		this.foreGroundColor = foreGroundColor;
		super.setForegroundColor(foreGroundColor);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLineStle()
	 */
	/**
	 * Gets the line stle.
	 * 
	 * @return the line stle
	 */
	public String getLineStle() {
		return this.lineStle;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setLineStle(java.lang.String)
	 */
	/**
	 * Sets the line stle.
	 * 
	 * @param lineStle the line stle
	 */
	public void setLineStle(String lineStle) {
		this.lineStle = lineStle;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLineThickness()
	 */
	/**
	 * Gets the line thickness.
	 * 
	 * @return the line thickness
	 */
	public int getLineThickness() {
		return this.LineThickness;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setLineThickness(int)
	 */
	/**
	 * Sets the line thickness.
	 * 
	 * @param lineThickness the line thickness
	 */
	public void setLineThickness(int lineThickness) {
		this.LineThickness = lineThickness;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshQualifiedName(com.cea.papyrus.diagraminterchange2.di2.GraphElement, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.uml2.uml.NamedElement)
	 */
	/**
	 * Refresh qualified name.
	 * 
	 * @param element the element
	 * @param currentge the currentge
	 * @param parentge the parentge
	 */
	public void refreshQualifiedName(GraphElement parentge, GraphElement currentge, NamedElement element){}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getFontColor()
	 */
	/**
	 * Gets the font color.
	 * 
	 * @return the font color
	 */
	public Color getFontColor() {
		return this.fontColor;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setFontColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * Sets the font color.
	 * 
	 * @param fontColor the font color
	 */
	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
		super.setForegroundColor(this.foreGroundColor);
	}
	
	/**
	 * this method is used to draw the icon in the middle of the link.
	 * 
	 * @param graphics @see package Graphics
	 */
	public void FillFigure(Graphics graphics) {
		graphics.pushState();
		MiddleEndpointLocator lo =new MiddleEndpointLocator (this,new Point(0,0));
//		point to translate
		Point start=this.getStart().getCopy();
		Point end=this.getPoints().getPoint(1).getCopy();
		start.y=start.y-this.img.getBounds().height/2;
		end.y=end.y-this.img.getBounds().height/2;
		start.x=start.x-this.img.getBounds().width/2;
		end.x=end.x-this.img.getBounds().width/2;
		Double angle= new Double(VectorCalculus.calculateAngle(start,end));
//		relaise rotation
		Point newMiddle=lo.getLocation().getCopy();
		double Ax=(new Double(newMiddle.x))-Math.cos(angle)*(this.img.getBounds().width/2);
		double Ay=(new Double(newMiddle.y))-Math.sin(angle)*(this.img.getBounds().width/2);
		double Ox=Ax+Math.cos(Math.PI/2-angle)*(this.img.getBounds().height/2);
		double Oy=Ay-Math.sin(Math.PI/2-angle)*(this.img.getBounds().height/2);
		newMiddle.x= (new Double(Ox)).intValue();
		newMiddle.y= (new Double(Oy)).intValue();
		graphics.translate(newMiddle);
		graphics.rotate((new Double(Math.toDegrees(angle))).intValue());
		graphics.drawImage(this.img, new Point(0,0));
		graphics.popState();
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshColors(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * Refresh colors.
	 * 
	 * @param graphElement the graph element
	 */
	public void refreshColors(GraphElement graphElement) {
//		set colors
		this.setBackgroundColor(PapyrusPlugin.colorManager.get(graphElement.getBackgroundColor()));
		this.setFontColor(PapyrusPlugin.colorManager.get( graphElement.getFontColor()));
		this.setBorderColor(PapyrusPlugin.colorManager.get(graphElement.getBorderColor()));
		this.setForeGroundColor(PapyrusPlugin.colorManager.get(graphElement.getFontColor()));
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshPresentation(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	public void refreshPresentation(String presentation, Element selection, GraphElement ge) {
		org.eclipse.swt.graphics.Image icon  = (new com.cea.papyrus.umlutils.Element(selection)).getIcon();
		/* if the presentation is icon or icon with text, we can set the icon */
		if ((icon != null) 
				&& ( (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION))
						|| presentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION)))
		{
			this.setIcon(icon);
			if (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION)) {
				return;
			}
		}
		return;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#refreshStereotypes(java.lang.String, java.lang.String, boolean, boolean)
	 */
	/**
	 * Refresh stereotypes.
	 * 
	 * @param presentation the presentation
	 * @param hasIcon the has icon
	 * @param hasShape the has shape
	 * @param stereotypes the stereotypes
	 */
	public void refreshStereotypes(String stereotypes, String presentation, boolean hasIcon, boolean hasShape) {
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setName(java.lang.String)
	 */
	/**
	 * Sets the name.
	 * 
	 * @param name the name
	 */
	public void setName( String name ){}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getNameLabel()
	 */
	/**
	 * Gets the name label.
	 * 
	 * @return the name label
	 */
	public Label getNameLabel(){
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getStereotypesLabel()
	 */
	/**
	 * Gets the stereotypes label.
	 * 
	 * @return the stereotypes label
	 */
	public Label getStereotypesLabel() {
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setQualifiedName(java.lang.String)
	 */
	/**
	 * Sets the qualified name.
	 * 
	 * @param packageName the package name
	 */
	public void setQualifiedName( String packageName )
	{}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getQualifiedNameLabel()
	 */
	/**
	 * Gets the qualified name label.
	 * 
	 * @return the qualified name label
	 */
	public Label getQualifiedNameLabel() {
		return null;}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#getLabelAt(org.eclipse.draw2d.geometry.Point)
	 */
	/**
	 * Gets the label at.
	 * 
	 * @param pt the pt
	 * 
	 * @return the label at
	 */
	public Label getLabelAt(Point pt) {
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.figures.IAbstractElementFigure#setStereotypes(java.lang.String)
	 */
	/**
	 * Sets the stereotypes.
	 * 
	 * @param stereotypes the stereotypes
	 */
	public void setStereotypes( String stereotypes )
	{}
	
	/**
	 * Stereotypes label.
	 * 
	 * @param editPart the edit part
	 * 
	 * @return the string
	 */
	public String stereotypesLabel(GraphEdgeEditPart editPart) {
		if(editPart.getGraphEdge().getSemanticModel().getPresentation().equals(ModelerHelper.STEREOTYPE_TEXT_VERTICAL_PRESENTATION)) {
			return editPart.verticalStereotypesToDisplay();
		} else {
			return editPart.horizontalStereotypesToDisplay();
		}
	}
	public Dimension getMinimumDimension() {
		return new Dimension(0,0);
	}
}
